import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Text2ImageTemplate from '@/views/template/Text2ImageTemplate.vue'
import Img2ImgTemplate from '@/views/template/Img2ImgTemplate.vue'
import TemplateShowcase from '@/views/template/TemplateShowcase.vue'
import ArtworksView from '@/views/ArtworksView.vue'
import MainLayout from '@/layouts/MainLayout.vue'
import AIDrawingWorkflowView from '@/views/AIDrawingWorkflowView.vue'
import LoginView from '@/views/LoginView.vue'
import RegisterView from '@/views/RegisterView.vue'
import DesignEditorView from '@/views/DesignEditorView.vue'
import SimpleDesignEditorView from '@/views/SimpleDesignEditorView.vue'
import DesignerView from '@/views/DesignerView.vue'
import OptimizedDesignerView from '@/views/OptimizedDesignerView.vue'
import RulerTabsDesignerView from '@/views/RulerTabsDesignerView.vue'
import NewDesignEditorView from '@/views/NewDesignEditorView.vue'
import SimpleEditorView from '@/views/SimpleEditorView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: LoginView,
      meta: { requiresAuth: false }
    },
    {
      path: '/register',
      name: 'register',
      component: RegisterView,
      meta: { requiresAuth: false }
    },
    {
      path: '/',
      component: MainLayout,
      children: [
        {
          path: '',
          redirect: '/home',
          meta: { requiresAuth: false }
        },
        {
          path: 'home',
          name: 'home',
          component: HomeView,
          meta: { requiresAuth: false }
        },
        {
          path: 'shopping',
          name: 'shopping',
          component: () => import('../views/ShoppingView.vue'),
          meta: { requiresAuth: false }
        },
        {
          path: 'tools',
          name: 'tools',
          component: () => import('../views/ToolsView.vue'),
          meta: { requiresAuth: false }
        },
        {
          path: 'entertainment',
          name: 'entertainment',
          component: () => import('../views/EntertainmentView.vue'),
          meta: { requiresAuth: false }
        },
        {
          path: 'life',
          name: 'life',
          component: () => import('../views/LifeView.vue'),
          meta: { requiresAuth: false }
        },
        {
          path: 'my',
          name: 'my',
          component: () => import('../views/MyView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: 'profile',
          name: 'user-profile',
          component: () => import('../views/UserProfileView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: 'artwork/:id',
          name: 'artwork-detail',
          component: () => import('../views/ArtworkDetailView.vue'),
          meta: { requiresAuth: false }
        },
        {
          path: 'ai-drawing',
          name: 'ai-drawing',
          component: AIDrawingWorkflowView,
          meta: { requiresAuth: false }
        },
        {
          path: 'artworks',
          name: 'artworks',
          component: ArtworksView,
          meta: { requiresAuth: false }
        },
        {
          path: 'workflows',
          name: 'workflows',
          component: () => import('../views/WorkflowManagement.vue'),
          meta: { requiresAuth: false }
        },
        {
          path: 'workflow/:id',
          name: 'workflow-detail',
          component: () => import('../views/WorkflowDetailView.vue'),
          meta: { requiresAuth: false }
        },
        {
          path: 'templates/showcase',
          name: 'template-showcase',
          component: TemplateShowcase,
          meta: { requiresAuth: false }
        },
        {
          path: 'templates/text2img',
          name: 'Text2ImageTemplate',
          component: Text2ImageTemplate,
          meta: { requiresAuth: false }
        },
        {
          path: 'templates/img2img',
          name: 'Img2ImgTemplate',
          component: Img2ImgTemplate,
          meta: { requiresAuth: false }
        },
        {
          path: 'workflow/create',
          name: 'workflow-create',
          component: () => import('../views/WorkflowCreateView.vue'),
          meta: { requiresAuth: false }
        },
        {
          path: 'design-editor',
          name: 'design-editor',
          component: SimpleEditorView,
          meta: { requiresAuth: false }
        },
        {
          path: 'simple-design-editor',
          name: 'simple-design-editor',
          component: SimpleDesignEditorView,
          meta: { requiresAuth: false }
        },
        {
          path: 'designer',
          name: 'designer',
          component: RulerTabsDesignerView,
          meta: { requiresAuth: false, title: 'AI设计工具' }
        },
        {
          path: 'optimized-designer',
          name: 'optimized-designer',
          component: OptimizedDesignerView,
          meta: { requiresAuth: false, title: '优化版设计工具' }
        },
        {
          path: 'old-designer',
          name: 'old-designer',
          component: DesignerView,
          meta: { requiresAuth: false, title: '原设计工具' }
        },
        {
          path: 'new-design-editor',
          name: 'new-design-editor',
          component: NewDesignEditorView,
          meta: { requiresAuth: false, title: '新设计编辑器' }
        },
        {
          path: 'simple-editor',
          name: 'simple-editor',
          component: SimpleEditorView,
          meta: { requiresAuth: false }
        }
      ]
    }
  ]
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
  // 检查页面是否需要登录
  const requiresAuth = to.meta.requiresAuth === true;
  const token = localStorage.getItem('token');

  // 如果页面需要登录且没有token，重定向到登录页
  if (requiresAuth && !token) {
    next({
      path: '/login',
      query: { redirect: to.fullPath }
    });
  } else {
    next();
  }
});

export default router